**Authoritarianism**

set more off
clear
capture log close
cd "/Users/fredsolt/Documents/Projects/Authoritarian Values/  Authoritarian6"
log using auth.log, replace

//Define locals for variables used
local dvs = "obey2 auth2 boss2" /*Change below also to allow standalone use of subroutines*/
local iv_c = "ethplur histcath confu excom" 
local iv_cy = "gini_net rgdpl dem dem_age"
local iv_i = "age educ income gini_netXincome female married children"


use "/Users/fredsolt/Documents/Projects/Data/WVS/xwvs+.dta", clear
local iv_is = regexr("`iv_i'", "gini_netXincome ", "")
keep s003 s025 country year `iv_is' a042 e018 c061
append using "/Users/fredsolt/Documents/Projects/Data/WVS/Waves 1-5/new_wvs1-5.dta", nolabel
keep s003 s025 country year `iv_is' a042 e018 c061

gen obey2=a042
gen auth2=(e018==1) if e018~=.
gen boss2=(c061==1) if c061~=.

replace country="United Kingdom" if country=="Great Britain"
replace country="Korea, Republic of" if country=="South Korea"
sort country year
merge country year using "/Users/fredsolt/Documents/Projects/Global Inequality/ SWIID v2/SWIIDv2_0.dta", ///
	 keep(gini_net gini_net_se) _merge(_m1)
drop if _m1==2

sort country year
merge country year using "/Users/fredsolt/Documents/Projects/Global Inequality/ SWIID v2/global7.dta", ///
	 keep(excom) _merge(_m2)
drop if _m2==2

replace excom=1 if country=="China"
replace excom=1 if country=="Viet Nam"
recode excom .=0

sort country year
merge country year using "/Users/fredsolt/Documents/Projects/Data/GDPpc/PWT63_cysort.dta", ///
	keep(rgdpl) _merge(_m3)
drop if _m3==2
replace country="Great Britain" if country=="United Kingdom"

replace rgdpl=4379.6 if country=="Belarus" & year==1990
replace rgdpl=8528.1 if country=="Latvia" & year==1990
replace rgdpl=9652.0 if country=="Lithuania" & year==1990
replace rgdpl=13208.82 if country=="Russian Federation" & year==1990
replace rgdpl=2237.129 if country=="Serbia and Montenegro" & year==1996
replace rgdpl=4105.18 if country=="Serbia and Montenegro" & year==2001
replace rgdpl=7244.14 if country=="Serbia" & year==2006

sort country
merge country using "/Users/fredsolt/Documents/Projects/Data/Religion/rel.dta", _merge(_m4)
drop if _m4==2
gen confu=(country=="China" | country=="Japan" | country=="Korea" | country=="Singapore"  ///
	| country=="Taiwan" | country=="Vietnam")

replace country="United Kingdom" if country=="Great Britain"
sort country year
merge country using "/Users/fredsolt/Documents/Projects/Data/Diversity/fractionalization.dta",  ///
	_merge(_m5) keep(ethnic)
rename ethnic ethplur
replace ethplur=ethplur*100
drop if _m5==2

replace ethplur=.026 if country=="Puerto Rico"
replace ethplur=.248 if country=="Serbia"

*Regime
sort country year
merge country year using "/Users/fredsolt/Documents/Projects/Data/Election Data/electoral.dta",  ///
	_merge(_mReg) keep(dem new dem_age dem_past)
drop if _mReg==2
recode dem .=0 if country=="Belarus" | country=="Estonia" | country=="Latvia"  ///
	| country=="Lithuania" | country=="Serbia" | country=="Russian Federation" | country=="Viet Nam"
recode dem .=1 if country=="Cyprus" | country=="Czech Republic" | country=="Puerto Rico"  ///
	| country=="Slovak Republic"
recode new .=0 if country=="Belarus" | country=="Estonia" | country=="Latvia"  ///
	| country=="Lithuania" | country=="Serbia" | country=="Russian Federation"  ///
	| country=="Viet Nam" | country=="Cyprus" | country=="Puerto Rico"
recode new .=1 if  country=="Czech Republic" | country=="Slovak Republic"
recode dem_age .=0 if dem==0
replace dem_age=(year+1)-1990 if dem_age==. & (country=="Czech Republic" | country=="Slovak Republic")
replace dem_age=(year+1)-1947 if dem_age==. & country=="Puerto Rico"
replace dem_age=(year+1)-1985 if dem_age==. & country=="Cyprus"
replace dem_age=70 if dem_age>70

recode dem_past .=0 if dem==1
recode dem_past .=1 if country=="Estonia" | country=="Latvia" | country=="Lithuania" | country=="Serbia"
recode dem_past .=0

replace country="Great Britain" if country=="United Kingdom"

replace income = income-3
replace educ = 18 if educ>18

save auth0.dta, replace
local v = "`iv_is' `iv_cy'"
while regexm("`v'", " ") {
	local v = regexr("`v'", " ", ",")
}
drop if mi(`v')
save auth0.dta, replace
keep country year s003 s025 `dvs' `iv_is' `iv_cy' `iv_c'
order country year s003 s025 `dvs' `iv_is' `iv_cy' `iv_c' 
recode s025 9001981=2761981
recode s003 900=276
sort s025
save auth.dta, replace

//Make datasets for HLM
if "`dvs'"=="" local dvs = "obey2 auth2 boss2" /*To allow standalone use of subroutine*/

foreach depvar in `dvs' {
	use auth.dta, clear
	drop if mi(`depvar')
	quietly ds `dvs' 
	gen vars=r(varlist)
	replace vars=regexr(vars, "`depvar'", "")
	local othvars=vars
	drop `othvars' vars
	sort s003 s025
	capture mkdir "`depvar'"
	outsheet using "`depvar'/`depvar'.csv", nolabel comma replace
	
	drop `iv_is' `depvar'
	duplicates drop s025, force
	save temp.dta, replace
	drop `iv_c'
	sort s003 s025
	outsheet using "`depvar'/`depvar'_cy.csv", nolabel comma replace
	
	use temp.dta, clear
	drop `iv_cy'
	duplicates drop s003, force
	sort s003 s025
	outsheet using "`depvar'/`depvar'_c.csv", nolabel comma replace	
	erase temp.dta

	shell sed "s/dv2/`depvar'/g" < mdmt2.mdmt > "`depvar'/`depvar'.mdmt" 
	shell sed "s/dv2/`depvar'/g" < mlm2.mlm > "`depvar'/`depvar'.mlm"
}

// Run HLM
use auth.dta, clear
foreach depvar in `dvs' {
	cd `depvar'

	hlm3cmd -r `depvar'.mdmt
	hlm3cmd `depvar'.mdm `depvar'.mlm

	cd ..
}

// Import results and calculate predicted probabilities
set more off
foreach depvar in `dvs' {
	cd `depvar'
	insheet using "`depvar'.csv", clear	
	gen cons=1
	gen gini_netXincome = gini_net*income
	
	if "`iv_c'"=="" local iv_c = "ethplur histcath confu excom" /*To allow standalone use of subroutine*/
	if "`iv_cy'"=="" local iv_cy = "gini_net rgdpl dem_age dem_past"
	if "`iv_i'"=="" local iv_i = "age educ income gini_netXincome female married children"
	
	local ivs = "cons `iv_c' `iv_cy' `iv_i'"
	local total_ivs = wordcount("`ivs'")	
	
	forvalues i = 1/`total_ivs' {
		local t = word("`ivs'", `i')
		if "`t'" == "gini_net" local gini_place = `i'
		if "`t'" == "income" local income_place = `i'
	}
	local giniXinc_place = `income_place'+1
	
	hlmimport `ivs', store(par)
	estsimp1
	
	save "`depvar'_1.dta", replace
	
	use "`depvar'_1.dta", clear
	
	//Create variables to store median values of all variables
	local i=0
	
	*Country vars
	collapse cons `iv_c', by(s003)
	
	foreach a of varlist cons `iv_c' {
		local ++i
		centile `a'
		gen v_`i'=r(c_1)
	}
	
	sort s003
	save "`depvar'_3.dta", replace
	
	*Country-year vars
	use "`depvar'_1.dta", clear
	
	collapse `iv_cy', by(s025)
	
	foreach a of varlist `iv_cy' {
		local ++i
		centile `a'
		gen v_`i'=r(c_1)
	}
	
	sort s025
	save "`depvar'_2.dta", replace
	
	*Individual vars
	use "`depvar'_1.dta", clear
	
	sort s003
	merge s003 using "`depvar'_3.dta", _merge(_m3)
	sort s025
	merge s025 using "`depvar'_2.dta", _merge(_m2)
	drop _m*
	
	foreach a of varlist `iv_i' {
		local ++i
		centile `a'
		gen v_`i'=r(c_1)
	}
	
	save "`depvar'_res.dta", replace
	
	//Generate baselines (no gini_net, income, incomeXgini_net terms in base1)
	gen base=0
	forvalues i=1/`total_ivs' {
		replace base=base+(v_`i' * c`i')
	}
	
	gen base1=0
	forvalues i=1/`total_ivs' {
		if `i'~=`gini_place' & `i'~=`income_place' & `i'~=`giniXinc_place' {
			replace base1=base1+(v_`i' * c`i')
		}
	}
	
	save "`depvar'_res.dta", replace
	erase "`depvar'_2.dta" 
	erase "`depvar'_3.dta"
	
	
	//table
	capture log close `depvar'_results
	log using "`depvar'_results.log", replace name("`depvar'_results")
	qui sum gini_net
	local min = r(min)
	local max = r(max)
	
	forvalues i = -2/2 {
		local ii = `i'+3
		qui gen est1lo_i`ii' = base1+`min'*c`gini_place'+`i'*c`income_place'+`i'*`min'*c`giniXinc_place'
		qui gen est1hi_i`ii' = base1+`max'*c`gini_place'+`i'*c`income_place'+`i'*`max'*c`giniXinc_place'
				
		qui gen pplo_i`ii'=1/(1+exp(-est1lo_i`ii'))
		qui gen pphi_i`ii'=1/(1+exp(-est1hi_i`ii'))
	
		qui gen diffpp_i`ii'=pphi_i`ii'-pplo_i`ii'
	
		di in white "for quintile " `ii'
		sum pplo_i`ii' pphi_i`ii' diffpp_i`ii'
		drop est1lo_i`ii' est1hi_i`ii'
	}
	
	*for comparison
	local j=0
	foreach iv in `ivs' {
		local j=`j'+1
		if `j'~=`gini_place' & `j'~=`income_place' & `j'~=`giniXinc_place' {
			qui sum `iv'
			local min = r(min)
			local max = r(max)
			
			qui gen estlo_`j'=base-v_`j'*c`j'+`min'*c`j'
			qui gen esthi_`j'=base-v_`j'*c`j'+`max'*c`j'
			
			qui gen pplo_`j'=1/(1+exp(-estlo_`j'))
			qui gen pphi_`j'=1/(1+exp(-esthi_`j'))
	
			qui gen diffpp_`j'=pphi_`j'-pplo_`j'
			
			di in white "`iv'" ", " `min' " to " `max'
			sum pplo_`j' pphi_`j' diffpp_`j'
			drop estlo_`j' esthi_`j'
		}
	}
	log close `depvar'_results

	//create data for graph
	*Based on Braumoeller's interaction routine
	qui sum gini_net
	local min=r(min)
	local max=r(max)
	local npts=15
	local inc=(`max'-`min')/(`npts'-1)
	matrix foo2 = 0,0,0,0,0,0,0,0,0,0  /*One zero for each parameter estimated, plus one*/
	local iter = 0
	while `iter' < =`npts' {
		local giniT = `min'+(`inc'*`iter')
		forvalues i = -2(2)2 {
			local ii = `i'+3
			qui gen est`ii'=base1+`giniT'*c`gini_place'+`i'*c`income_place'+`i'*`giniT'*c`giniXinc_place'
			qui gen ppest`ii'=1/(1+exp(-est`ii'))
			sum ppest`ii'
			scalar ppe`ii'=r(mean)
			scalar ppl`ii'=ppe`ii'-1.95*r(sd)
			scalar ppu`ii'=ppe`ii'+1.95*r(sd)
		}
		*save values to matrix	
		matrix foo = ppe1, ppl1, ppu1, ppe3, ppl3, ppu3, ppe5, ppl5, ppu5, `giniT'
		matrix foo2 = foo2 \ foo
		drop est1 ppest1 est3 ppest3 est5 ppest5
		local iter=`iter'+1
	}
	
	*convert matrix to data
	matrix pts=foo2[2..(`npts'+1),1..10]
	svmat pts

	save "`depvar'_res.dta", replace

	cd ..
}

//Predicted Probability Graphs
local lab_obey2=`""Considers Learning" "Obedience Important""'
local lab_auth2=`""Prefers More" "Respect for Authority""'
local lab_boss2=`""Says One Should" "Always Obey Boss""'

foreach depvar in `dvs' {
	cd `depvar'
	use "`depvar'_res.dta", clear
	twoway connect pts4 pts5 pts6 pts10, lp(solid dash dash) lc(black gs8 gs8) m(i i i) ///
		legend(off) xtitle("") ylabel(0(.25)1) ///
		ytitle(`lab_`depvar'', size(medlarge) margin(r+5) orientation(horizontal) width(25))  ///
		graphregion(lcolor(white) color(white)) nodraw
	cd ..
	graph save pp_`depvar'.gph, replace
}

graph combine pp_obey2.gph pp_auth2.gph pp_boss2.gph, ///
	cols(1) ycommon xcommon imargin(vsmall)  ///
	graphregion(margin(l=5) color(white) lcolor(white)) xsize(6) ysize(9) ///
	title("Income Inequality", size(medsmall) width(55) justification(center) ring(5) pos(5))
graph save pp_combined.gph, replace
graph export pp_combined.ps, replace logo(off)

//Results Dotplot
use auth, clear
set more off
if "`dvs'"=="" local dvs = "obey2 auth2 boss2" /*To allow standalone use of subroutine*/
if "`iv_c'"=="" local iv_c = "ethplur histcath confu excom"
if "`iv_cy'"=="" local iv_cy = "gini_net rgdpl dem dem_age"
if "`iv_i'"=="" local iv_i = "age educ income gini_netXincome female married children"
if "`ivs'"=="" local ivs = "cons `iv_c' `iv_cy' `iv_i'"
local total_ivs = wordcount("`ivs'")	
forvalues i = 1/`total_ivs' {
	local t = word("`ivs'", `i')
	if "`t'" == "gini_net" local gini_place = `i'
	if "`t'" == "income" local income_place = `i'
}
local giniXinc_place = `income_place'+1


gen ivno = _n in 1/`total_ivs'
keep ivno
drop if ivno==.
save dotplot.dta, replace

foreach depvar in `dvs' {
	use "`depvar'/`depvar'_res.dta", clear
	qui gen i_var = ""
	qui gen ivno = .
	qui gen b_`depvar' = .
	qui gen b_`depvar'_lb = .
	qui gen b_`depvar'_ub = .
	centile gini_net
	gen c`income_place'a = c`income_place'+c`giniXinc_place'*r(c_1)
	
	local i = 1
	foreach v in `ivs' {
		replace i_var = "`v'" in `i'
		replace ivno = `i'
		sum `v'
		if `r(min)'~=0 & `r(max)'~=1 local sdX2 = 2*`r(sd)'
		else local sdX2 = 1
		if `i'~= `income_place' sum c`i' 
		else sum c`i'a
		
		replace b_`depvar' = `r(mean)'*`sdX2' in `i'
		replace b_`depvar'_lb = (`r(mean)'-`r(sd)'*invnorm(.975))*`sdX2' in `i'	
		replace b_`depvar'_ub = (`r(mean)'+`r(sd)'*invnorm(.975))*`sdX2' in `i'
	
		local i = `i'+1
	}
	
	keep ivno i_var b_`depvar' b_`depvar'_lb b_`depvar'_ub
	drop if b_`depvar'==.
	sort ivno
	save "`depvar'/`depvar'_est.dta", replace
	use dotplot.dta, clear
	sort ivno
	merge ivno using "`depvar'/`depvar'_est.dta", _merge(_`depvar')
	save dotplot.dta, replace
}

drop if b_obey2==.
keep i_var b*
save dotplot.dta, replace


//Scatterplots
use auth.dta, clear
collapse `dvs' gini_net, by(s025)

foreach depvar of varlist  `dvs' {
	replace `depvar' = `depvar'*100
}

save auth_cy.dta, replace

use auth_cy.dta, clear
local lab_obey2=`""Considers Learning" "Obedience Important""'
local lab_auth2=`""Prefers More" "Respect for Authority""'
local lab_boss2=`""Says One Should" "Always Obey Boss""'
foreach depvar in `dvs' {
	quietly corr gini_net `depvar'
	local R = round(`r(rho)', .01)
	twoway (scatter `depvar' gini_net, msize(vsmall) text(7.5 55 "R = `R'", box  ///
		just(left) margin(l+1.5 t+.5 b+1) width(10)) legend(off)) ///
		(lfit `depvar' gini_net, lcolor(gs8)), ///
		xtitle("") ytitle(`lab_`depvar'', size(medlarge) margin(r+5) orientation(horizontal) width(25)) ///
		yscale(range(0 100)) ylabel(0(25)100) graphregion(lcolor(white) color(white)) nodraw
	graph save gr_`depvar'.gph, replace
}

graph combine gr_obey2.gph gr_auth2.gph gr_boss2.gph, ///
	cols(1) xcommon imargin(vsmall)  ///
	graphregion(margin(l=5) color(white) lcolor(white)) xsize(6) ysize(9) ///
	title("Income Inequality", size(medsmall) width(55) justification(center) ring(5) pos(5))
graph save gr_combined.gph, replace
graph export gr_combined.ps, replace logo(off)


//Appendix
use auth.dta, clear
levelsof s003, local(countries)
gen yrs = ""
foreach c of local countries {
	quietly levelsof year if s003==`c', local(years) separate(", ")
	quietly replace yrs = "`years'" if s003==`c'
}
duplicates drop country yrs, force
keep country yrs
sort country
list, noobs clean
save auth_app.dta, replace


set more on
log close

